package com.odm.communitymanage.ui.pay

import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.bean.TodayPrice
import com.zhengsr.tablib.view.adapter.BaseFlowAdapter
import com.zhengsr.tablib.view.adapter.LabelFlowAdapter
import kotlinx.android.synthetic.main.fragment_expense.*


/**
 * @description: 缴费页面（首页进入）
 * @author: ODM
 * @date: 2020/4/20
 */
class ExpenseFragment : BaseFragment() {

    private var rvAdapter : TodayPriceAdapter ?= null



    override fun onSupportInvisible() {
        super.onSupportInvisible()

    }



    override fun initViews() {
        initRecyclerView()

        //标题返回事件
        btnBackExpense?.setOnClickListener {
            pop()
        }
        //“立刻缴费”的点击事件
        tvPayInstantly?.setOnClickListener {
            ToastUtils.showShort("即将跳转支付页面")
        }

        initBarChart2()

        initTypeLabelLayout()

    }


    fun initRecyclerView(){
        //横向布局
        rvTodayPrice?.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
        rvAdapter = TodayPriceAdapter(mutableListOf())
        rvTodayPrice?.adapter = rvAdapter
        rvAdapter?.animationEnable = true
        rvAdapter?.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInRight)
        //加载假数据
        loadTodayPriceData()
    }

    fun initTypeLabelLayout(){
        val mTitles : List<String> = arrayListOf("电费","水费","燃气费","停车费")
        var adapter: LabelFlowAdapter<*>
        flTypeName_Expense?.setAdapter(object : LabelFlowAdapter<String?>(R.layout.item_label_text, mTitles) {
            override fun bindView(view: View?, data: String?, position: Int) {
                setText(view, R.id.item_label, data)
                    .setTextColor(view,R.id.item_label,resources.getColor(R.color.label_text_unselected))
                // 注册子控件的点击事件
//                addChildrenClick(view,R.id.item_text,position)
            }

            override fun onItemSelectState(view: View, isSelected: Boolean) {
                super.onItemSelectState(view, isSelected)
                val textView: TextView = view.findViewById(R.id.item_label)
                if (isSelected) {
                    textView.setTextColor(Color.WHITE)
                    textView.background = resources.getDrawable(R.drawable.shape_label_item_expense)
                } else {
                    //未选中时的textView颜色和背景
                    textView.setTextColor(ColorUtils.getColor(R.color.label_text_unselected))
                    textView.background = resources.getDrawable(R.drawable.shape_label_unselected_expense)
                }
            }

            override fun onItemClick(view: View?, data: String?, position: Int) {
                super.onItemClick(view, data, position)
                when(position){
                    0->{
                        val price = 0.88
                        val data1 = intArrayOf(105, 120, 80, 75, 132)
                        val data2 =
                            intArrayOf((data1[0] * price).toInt(), (data1[1]* price).toInt(), (data1[2]*price).toInt(), (data1[3]*price).toInt(), (data1[4]*price).toInt())
                        setBarChart2FakeData(data1,data2)
                        //修改 右上角本月数据
                        val needValue = 100
                        tvTypeValue_Expense.text = "${needValue} 度"
                        tvNeedPriceValue_Expense.text = "${needValue*price} 元"
                    }

                    1->{
                        val price = 3.50
                        val data1 =
                            intArrayOf(15, 32, 8, 25,38 )
                        val data2 =
                            intArrayOf((data1[0] * price).toInt(), (data1[1]* price).toInt(), (data1[2]*price).toInt(), (data1[3]*price).toInt(), (data1[4]*price).toInt())
                        setBarChart2FakeData(data1,data2)
                        //修改 右上角本月数据
                        val needValue = 23
                        tvTypeValue_Expense.text = "${needValue} 吨"
                        tvNeedPriceValue_Expense.text = "${needValue*price} 元"
                    }

                    2->{
                        val price = 2.64
                        val data1 =
                            intArrayOf(45, 50, 30, 35, 40)
                        val data2 =
                            intArrayOf((data1[0] * price).toInt(), (data1[1]* price).toInt(), (data1[2]*price).toInt(), (data1[3]*price).toInt(), (data1[4]*price).toInt())
                        setBarChart2FakeData(data1,data2)
                        //修改 右上角本月数据
                        val needValue = 35
                        tvTypeValue_Expense.text = "${needValue} m³"
                        tvNeedPriceValue_Expense.text = "${needValue*price} 元"
                    }

                    3->{
                        val price : Double = 4.50
                        val data1 =
                            intArrayOf(25, 20, 40, 35, 30)
                        val data2 =
                            intArrayOf((data1[0] * price).toInt(), (data1[1]* price).toInt(), (data1[2]*price).toInt(), (data1[3]*price).toInt(), (data1[4]*price).toInt())
                        setBarChart2FakeData(data1,data2)
                        //修改 右上角本月数据
                        val needValue = 20
                        tvTypeValue_Expense.text = "$needValue 时"
                        tvNeedPriceValue_Expense.text = "${needValue*price} 元"
                    }
                }
            }
        }.also {
            adapter = it

        })
        flTypeName_Expense?.setSelects(0)
        //修改 右上角本月数据
        val needValue = 100
        tvTypeValue_Expense.text = "${needValue} 度"
        tvNeedPriceValue_Expense.text = "${needValue*0.88} 元"
    }



    fun loadTodayPriceData(){
        val data : MutableList<TodayPrice>  = mutableListOf()
        data.add(TodayPrice(price = "3.50元/吨",type = 0,name = "今日水价"))
        data.add(TodayPrice(price = "0.88元/度",type = 1,name = "今日电价"))
        data.add(TodayPrice(price = "2.64元/立方米",type = 2,name = "今日燃气价"))
        data.add(TodayPrice(price = "4.50元/时",type = 3,name = "今日泊车价"))
        rvAdapter?.setNewData(data)
    }


    /**
     * 初始化柱状图2数据
     */
    private fun initBarChart2() {
        val xLabel = arrayOf(
            "0", "1", "2", "3", "4", "5"
        )
        val yLabel = arrayOf(
            "0", "40", "80", "120", "160", "200"
        )
        //x轴数值数组长度需要比x轴label少一，因为x轴label是从0才开始绘制了
        val data1 =
            intArrayOf(105, 120, 80, 75, 102)
        val data2 =
            intArrayOf((105.0* 0.88).toInt(), (120.0*0.88).toInt(), (80.0*0.88).toInt(), (70.0*0.88).toInt(), (102.0*0.88).toInt())
        val data: MutableList<IntArray> = ArrayList()
        data.add(data1)
        data.add(data2)
        val color: MutableList<Int> = ArrayList()
        color.add(R.color.common_price_orange)
        color.add(R.color.common_price_green)
        color.add(R.color.common_price_blue)
        bcMonthExpense.setInitData(xLabel, yLabel, data, color)
    }

    private fun setBarChart2FakeData(data1:IntArray ,data2:IntArray){
        //x轴数值数组长度需要比x轴label少一，因为x轴label是从0才开始绘制了

        val data: MutableList<IntArray> = ArrayList()
        data.add(data1)
        data.add(data2)
        bcMonthExpense.dataList = data
    }





    override fun getLayoutId(): Int {
        return R.layout.fragment_expense
    }

}